package com.sfilyh.servelt;

import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sfilyh.util.DBUtil;

@WebServlet("/view")
public class ArticleDetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id =  request.getParameter("id");
		String error = request.getParameter("error");
		if(error != null || "".equals(error)){
			request.setAttribute("error", true);
		}
		Integer article = 0;
		if(id != null && !"".equals(id)){
			article = Integer.parseInt(id);
		}
		// 文章详情
		String sql = "SELECT A.*,B.cname, DATE_FORMAT(A.createtime,'%Y年%m月%d日 %k点%i分') AS ftime FROM tb_article A ,tb_category B WHERE A.category = B.id and A.id = ? ";
		Map<String,Object> articleMap =  DBUtil.queryForMap(sql, article);
		
		// 导航栏显示 
		if(articleMap.get("status") != null && articleMap.get("status").toString().equals("0")){
			request.setAttribute("cid", articleMap.get("category"));
		}else if(articleMap.get("status") != null && articleMap.get("status").toString().equals("2")){
			request.setAttribute("articleid", articleMap.get("id"));
		}
		
		request.setAttribute("article", articleMap);
		
		/** 评论详情
		String postSql ="select * from tb_post where articleid = ? ";
		List<Map<String, Object>> rList = DBUtil.queryForListMap(postSql, article);
		request.setAttribute("postList", rList);
		
		if(rList != null){
			// 二级回复
			Map<String,List<Map<String,Object>>> postList = new LinkedHashMap<String,List<Map<String,Object>>>();
			for (Map<String, Object> map : rList) {
				Object obj =  map.get("pid");
				if(obj != null && !obj.toString().equals("0")){
					if(postList.containsKey(obj.toString())){
						postList.get(obj.toString()).add(map);					
					}else{
						List<Map<String,Object>> tMap = new ArrayList<Map<String,Object>>();
						tMap.add(map);
						postList.put(obj.toString(), tMap);						
					}
					// 移去
					map.put("remove", true);
				}
			}
			request.setAttribute("rePostList", postList);
		}**/
		request.getRequestDispatcher("/WEB-INF/views/view.jsp").forward(request, response);		
	}
}
